/**
 * 
 */
package com.scpii.universal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.CommentManageDto;
import com.scpii.universal.hibernate.dao.CommentDao;
import com.scpii.universal.hibernate.dao.CommentManageDao;
import com.scpii.universal.hibernate.entity.CommentTab;
import com.scpii.universal.service.CommentManageService;

/**
 * 
 * genericWeb
 * 
 * @author weny 2012-7-5下午05:33:09
 */
@Service
public class CommentManageServiceImpl extends
		BaseServiceImpl<CommentManageDto, Integer> implements
		CommentManageService {
	@Resource
	private CommentManageDao CommentManageDao;
   @Resource
   private CommentDao CommentDao;
	@Resource
	public void setBaseDao(CommentManageDao CommentManageDao) {
		super.setBaseDao(CommentManageDao);
	}

	/**
	 * 查找指定应用的内容评论
	 * 
	 * @param intAppId
	 * @return
	 * @author zhangqiang 2012-7-26下午03:35:30
	 */
	@Override
	public List<CommentManageDto> getCommentInfo(Integer intAppId,CommentManageDto condition, int pageNo, int pageSize) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.CommentManageDto(t1.intCommentId,");
		hql.append(" t1.intUserId,");
		hql.append(" t1.intContentId,");
		hql.append(" t1.varCommentName,");
		hql.append(" t1.varCommentContent,");
		hql.append(" t1.datCommentDatetime,");
		hql.append(" t2.varContentName,");
		hql.append(" t3.varUserLoginName,");
		hql.append(" t3.varUserPhonenum )");
		hql.append(" from CommentTab t1 , ");
		hql.append("  ContentTab t2, ");
		hql.append("  ClientUserTab t3 ");
		hql.append(" where t1.intUserId= t3.intUserId ");
		hql.append(" and t1.intContentId=t2.intContentId ");
		hql.append(" and t2.intAppId=:intAppId ");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
		hql.append(" and t3.intDeleteSign=:intDeleteSign ");

		Map<String, Object> argsMap = new HashMap<String, Object>();

		argsMap.put("intAppId", intAppId);
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		
		//查询条件
		if(condition!=null){
			//评论内容
			if(condition.getVarCommentContent()!=null&&!"".equals(condition.getVarCommentContent())){
				
				hql.append(" and t1.varCommentContent like :varCommentContent");
				argsMap.put("varCommentContent", "%"+condition.getVarCommentContent()+"%");
			}
			//内容标题
			if(condition.getVarContentName()!=null&&!"".equals(condition.getVarContentName())){
				
				hql.append(" and t2.varContentName like :varContentName");
				argsMap.put("varContentName", "%"+condition.getVarContentName()+"%");
			}
			
		}
		hql.append(" order by t1.datCommentDatetime desc,t1.intCommentId desc");

		return CommentManageDao.findByHql(hql.toString(),pageNo,pageSize, argsMap);
	}

	@Override
	public int getCountByCondition(Integer intAppId,CommentManageDto condition) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.CommentManageDto(t1.intCommentId)");
		
		hql.append(" from CommentTab t1 , ");
		hql.append("  ContentTab t2, ");
		hql.append("  ClientUserTab t3 ");
		hql.append(" where t1.intUserId= t3.intUserId ");
		hql.append(" and t1.intContentId=t2.intContentId ");
		hql.append(" and t2.intAppId=:intAppId ");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
		hql.append(" and t3.intDeleteSign=:intDeleteSign ");

		Map<String, Object> argsMap = new HashMap<String, Object>();

		argsMap.put("intAppId", intAppId);
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		
		//查询条件
		if(condition!=null){
			//评论内容
			if(condition.getVarCommentContent()!=null&&!"".equals(condition.getVarCommentContent())){
				
				hql.append(" and t1.varCommentContent like :varCommentContent");
				argsMap.put("varCommentContent", "%"+condition.getVarCommentContent()+"%");
			}
			//内容标题
			if(condition.getVarContentName()!=null&&!"".equals(condition.getVarContentName())){
				
				hql.append(" and t2.varContentName like :varContentName");
				argsMap.put("varContentName", "%"+condition.getVarContentName()+"%");
			}
			
		}
		List<CommentManageDto> cmList=CommentManageDao.hqlGetList(hql.toString(), argsMap);
		if(cmList!=null){
			return cmList.size();
		}else {
			return 0;
		}
	}
	/**
	 * 逻辑删除评论
	 * @param intCommentId
	 * @author zhangqiang
	 * 2012-7-26下午04:13:00
	 */
	public void updateCommentById(Integer intCommentId){
		CommentTab commTab=CommentDao.get(intCommentId);
		commTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_DELETED);
		CommentDao.update(commTab);
	}
}
